package com.andnnl.file;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by chenss on 2019/12/18.
 */
public class StatusHandle {
    public static void main(String[] args) {
//        test();
//
        todo();
    }

    private static void test() {
        String s = "[11-04 04:00:27,753 INFO ] [Thread-16] SubmitLongMsgIdManage.saveToRedis(165) : save long msgid to redis,msgid:02506511040359537456_18123760238,parentMsgid:02506511040359537451_18123760238,index:6";
        Pattern regex = Pattern.compile(",msgid:(.+?)_(.+?),", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
        List<String> rs = ReUtil.getAllGroups(regex, s, false);
        System.out.println(rs);

    }

    private static Map<String, String> getMap() {
        Map<String, String> map = new HashMap<>();
        try (BufferedReader bf = new BufferedReader(new FileReader("E:\\20028_a\\20028_msgid.log"))) {
            Pattern regex = Pattern.compile(",msgid:(.+?)_(.+?),", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
            String str;
            while ((str = bf.readLine()) != null) {
                if (StrUtil.isBlank(str)) continue;
                List<String> rs = ReUtil.getAllGroups(regex, str, false);
                map.put(rs.get(0), rs.get(1));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return map;
    }

    private static void todo() {
        try {
            FileReader fr = new FileReader("E:\\20028_a\\20028_msgid.log");
            FileReader frSf = new FileReader("E:\\20028_a\\20028_msgid_sf.log");
            FileReader fr2 = new FileReader("E:\\20028_a\\20028_status.log");
            FileWriter fw = new FileWriter("E:\\20028_a\\20028_status2.log");
            BufferedReader bf = new BufferedReader(fr);
            BufferedReader bfSf = new BufferedReader(frSf);
            BufferedReader bf2 = new BufferedReader(fr2);
            BufferedWriter bw = new BufferedWriter(fw);
            String str;
            int i = 1;
            //[11-04 04:00:27,753 INFO ] [Thread-16] SubmitLongMsgIdManage.saveToRedis(165) : save long msgid to redis,msgid:02506511040359537456_18123760238,parentMsgid:02506511040359537451_18123760238,index:6
            Pattern regex = Pattern.compile(",msgid:(.+?)_(.+?),", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
            Pattern regexSf = Pattern.compile("key:SF:(.+?)_(.+?) ", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
            Pattern regex2 = Pattern.compile("\"smsMsgid\":\"(.+?)_000000000\"", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);

            Map<String, String> map = new HashMap<>();
            Map<String, String> mapSf = new HashMap<>();
            // 按行读取字符串
            while ((str = bf.readLine()) != null) {
                if (StrUtil.isBlank(str)) continue;
                List<String> rs = ReUtil.getAllGroups(regex, str, false);
                map.put(rs.get(0), rs.get(1));
            }            // 按行读取字符串
            while ((str = bfSf.readLine()) != null) {
                if (StrUtil.isBlank(str)) continue;
                List<String> rs = ReUtil.getAllGroups(regexSf, str, false);
                mapSf.put(rs.get(0), rs.get(1));
            }
            while ((str = bf2.readLine()) != null) {
                if (StrUtil.isBlank(str)) continue;
                String rs = ReUtil.getGroup1(regex2, str);
                String mobile = map.get(rs);
                if (mobile != null) {
                    str = str.replaceAll("000000000", mobile);
//                    System.out.println(str);
                    bw.write(str);
                    bw.newLine();
                } else {
                    String mobileSf = mapSf.get(rs);
                    if (mobileSf != null) {
                        str = str.replaceAll("000000000", mobileSf);
                    }
                    System.out.println(str);
                }

//                map.put(rs.get(0),rs.get(1));
            }
            bf.close();
            fr.close();
            bw.close();
            fw.close();
            bf2.close();
            fr2.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
